 package tgc.edu.yzy.web.controller;

import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.yzy.service.AdminService;
import tgc.edu.yzy.service.AdministatorService;
import tgc.edu.yzy.service.RecordService;
import tgc.edu.yzy.service.TradingRecordService;
import tgc.edu.yzy.service.UserService;
import tgc.edu.yzy.web.form.AdminForm;
import tgc.edu.yzy.web.form.TradingFrom;
import tgc.edu.yzy.custom.Custom;
import tgc.edu.yzy.custom.JpgridUtils;
import tgc.edu.yzy.custom.JqgridParam;
import tgc.edu.yzy.entity.EimAdmin;
import tgc.edu.yzy.entity.EimAdministrator;
import tgc.edu.yzy.entity.EimRegister;
import tgc.edu.yzy.entity.EimUser;
import tgc.edu.yzy.entity.Record;
import tgc.edu.yzy.entity.TradingRecord;
import tgc.edu.yzy.repostory.EimRegisterRepository;
import tgc.edu.yzy.security.User2;

@Controller
@RequestMapping(value="/admini")
public class AdminController {
	@Autowired
	private AdminService admin;
	@Autowired
	private AdministatorService eimadmin;
	@Autowired
	private TradingRecordService record;
	@Autowired
	private RecordService recordservice;
	@Autowired
	private UserService userservice;
	@Autowired
	private EimRegisterRepository userDAO; 
	@Autowired
	private Custom custom;
	
	/***
	 * Ajax 显示所有系统管理员
	 * @param param
	 * @return
	 */
	@RequestMapping(value="list")
	@ResponseBody
	private Object list(JqgridParam param) {
		Pageable pageable = param.buildPageable();
		Page<EimAdmin> page=null;
		if(!param.getSearch()) {
			page=admin.findAll(pageable);
		}else {
			page=admin.findAll(param.buildSpec(), pageable);
		}
		HashMap<String , Object> result = JpgridUtils.getPageResult(page);
		return result;
	}
	/***
	 * 修改管理员的时间
	 * @param id
	 * @param form
	 * @param feiyong
	 * @param startdate
	 * @return
	 */
	@RequestMapping(value="/admin1edit")
	@ResponseBody
	public Object edit(String id,AdminForm form,Integer feiyong,Date startdate) {
		EimAdmin find = admin.findById(Integer.parseInt(id));
		
		TradingRecord tradings=new TradingRecord();
		tradings.setStarttime(startdate);
		tradings.setEndtime(form.getMaxdate());
		tradings.setMoney(feiyong);
		tradings.setEimadmins(find);
		tradings.setMaxdate(find.getMaxdate());
		record.save(tradings);
		
		List<EimUser> eimu=userservice.findByEimadmins(find);
		for (EimUser e : eimu) {
			e.setMaxdate(form.getMaxdate());
			userservice.save(e);
		}
		find.setMaxdate(form.getMaxdate());
		admin.save(find);
		
		return "随意输入";
	}
	
	/***
	 * 显示所有增加时间的记录
	 * @param param
	 * @return
	 */
	@RequestMapping(value="tradinglist")
	@ResponseBody
	private Object tradinglist(JqgridParam param) {
		Pageable pageable = param.buildPageable();
		Page<TradingRecord> page=null;
		if(!param.getSearch()) {
			page=record.findAll(pageable);
		}else {
			page=record.findAll(param.buildSpec(), pageable);
		}
		HashMap<String , Object> result = JpgridUtils.getPageResult(page);
		return result;
	} 
	/***
	 * 修改和删除授权时间记录
	 * @param id
	 * @param form
	 * @param endtime
	 * @return
	 */
	@RequestMapping(value="/tradingedit")
	@ResponseBody
	public Object tradingedit(String id,TradingFrom form,Date endtime) {
		TradingRecord records=record.findById(Integer.parseInt(id));
		String oper =form.getOper();
		if("del".equals(oper)) {
			/*Integer a=record.getIds();
			
			System.out.println("这里输出的a是："+a);*/
			/*EimAdmin eimadmins = records.getEimadmins();
			eimadmins.setMaxdate(records.getMaxdate());
			admin.save(eimadmins);
			record.deleteByID(Integer.parseInt(id));*/
		}else{
			records.setEndtime(form.getEndtime());
			records.setMoney(form.getMoney());
			records.setStarttime(form.getStarttime());
			record.save(records);
			EimAdmin eimadmins = records.getEimadmins();
			eimadmins.setMaxdate(form.getEndtime());
			admin.save(eimadmins);
		}
		return "随意输入";
	}
	/***
	 * 显示所有操作日志
	 * @param param
	 * @return
	 */
	@RequestMapping(value="recordlist")
	@ResponseBody
	private Object record(JqgridParam param) {
		Pageable pageable = param.buildPageable();
		Page<Record> page=null;
		if(!param.getSearch()) {
			page=recordservice.findAll(pageable);
		}else {
			System.out.println("查询ID"+param.getSearchField()+"\r查询的类型"+param.getSearchOper()+
					"\r查询参数"+param.getSearchString()+"\r查询接受参数"+param.getFilters());
			page=recordservice.findAll(param.buildSpec(), pageable);
		}
		HashMap<String , Object> result = JpgridUtils.getPageResult(page);
		return result;
	} 
	//删除某一条记录
	@RequestMapping(value="/recordedit")
	@ResponseBody
	public Object recordedit(String id) {
		recordservice.deleteByID(Integer.parseInt(id));
		return "随意输入";
	}
	/***
	 * Ajax 查询是否有这个用户
	 * @param session
	 * @param usern
	 * @return
	 */
	@RequestMapping(value="usern")
	@ResponseBody
	private Boolean usern(HttpSession session,String usern) {
		EimRegister usernamess = userDAO.findByUsername(usern);//根据账号查找出用户
		if(usernamess!=null) {
			Integer user2 = custom.getUser2();
			if(usernamess.getId()!=user2) {
				return false;
			}else {
				session.setAttribute("admin", usernamess);
				return true;
			}
		}
		return false;
	}
	/***
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value="time")
	private String time(Integer id,ModelMap map) {
		EimAdmin findById = admin.findById(id);
		java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
		System.out.println(currentDate);
		System.out.println(findById.getMaxdate());
		map.put("admin", findById.getZhmc());
		map.put("adminid", findById.getId());
		if(currentDate.after(findById.getMaxdate())){
			map.put("times", currentDate);
			map.put("time", findById.getMaxdate());
		}else {
			map.put("times", findById.getMaxdate());
			map.put("time", findById.getMaxdate());
		}
		return "home/views/accredit";
	}
	/***
	 * 新增
	 * @param map
	 * @param number
	 * @param form
	 * @param admins
	 * @return
	 */
	@RequestMapping(value="/add")
	private String add(ModelMap map,String number,TradingFrom form,Integer admins) {
		EimAdmin adminall = admin.findById(admins);
		if(number==null||number=="") {
			TradingRecord recor=new TradingRecord();
			BeanUtils.copyProperties(form, recor);
			recor.setEimadmins(adminall);
			record.save(recor);
			
			List<EimUser> eimu=userservice.findByEimadmins(adminall);
			for (EimUser e : eimu) {
				e.setMaxdate(form.getEndtime());
				userservice.save(e);
			}
			
			adminall.setMaxdate(form.getEndtime());
			admin.save(adminall);
		}else {
			TradingRecord recor=new TradingRecord();
			BeanUtils.copyProperties(form, recor);
			recor.setEimadmins(adminall);
			
			
			Date start = form.getStarttime();
			Calendar calendar =new GregorianCalendar();
			
			calendar.setTime(start);
			java.util.Date utilDate = (java.util.Date)calendar.getTime();
			calendar.add(Calendar.DATE, Integer.parseInt(number));
			utilDate = (java.util.Date)calendar.getTime();
			Date newDate =new Date(utilDate.getTime());
			
			recor.setEndtime(newDate);
			record.save(recor);
			
			adminall.setMaxdate(newDate);
			admin.save(adminall);
			List<EimUser> eimu=userservice.findByEimadmins(adminall);
			for (EimUser e : eimu) {
				e.setMaxdate(newDate);
				userservice.save(e);
			}
			
		}
		
		return "redirect:../users";
	}
}
